package com.android.uu.view;

import java.io.Serializable;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 地区（城市）信息
 */
public class District implements Serializable {

	private static final long serialVersionUID = 1L;

//	        "id": "234",
//			"name": "莱芜市",
//			"parentid": "15",
//			"nodepath": "0,15",
//			"namepath": "山东省,莱芜市",
//			"charindex": "L",
//			"level": "2",
//			"orderby": "1"

	private String id;// 主键id
	private String name;// 地区名称
	private String parentid;// 父级别主键
	private String nodepath;// 节点主键路径串
	private String namepath;// 节点名称路径串
	private String charindex;// 名称拼音首字母索引 比如"北京"的charindex为 "B"
	private String level;// 节点层级 一个整数
	private String orderby;// 排序优先级
	
	public District() {
		
	}

	public void setName(String name) {
		this.name = name;
	}


	public District(JSONObject jsonObject) throws Exception {
		if (jsonObject != null) {
			try {
				id = get(jsonObject, "id");
				name = get(jsonObject, "name");
				parentid = get(jsonObject, "parentid");
				nodepath = get(jsonObject, "nodepath");
				namepath = get(jsonObject, "namepath");
				charindex = get(jsonObject, "charindex");
				level = get(jsonObject, "level");
				orderby = get(jsonObject, "orderby");

			} catch (JSONException e) {
				throw new Exception(e);
			}
		}
	}
	/**
     * 解析时，判断是否为空
     * 
     * @param jsonObject
     * @param s
     * @return
     * @throws JSONException
     */
    protected String get(JSONObject jsonObject, String s) throws JSONException {
        if (!jsonObject.isNull(s)) {
            return jsonObject.getString(s);
        }
        return null;
    }

	/**
	 * @param id
	 * @param name
	 * @param parentid
	 * @param nodepath
	 * @param namepath
	 * @param charindex
	 * @param level
	 * @param orderby
	 */
	public District(String id, String name, String parentid, String nodepath,
			String namepath, String charindex, String level, String orderby) {
		super();
		this.id = id;
		this.name = name;
		this.parentid = parentid;
		this.nodepath = nodepath;
		this.namepath = namepath;
		this.charindex = charindex;
		this.level = level;
		this.orderby = orderby;
	}

	@Override
	public String toString() {
		return "District [id=" + id + ", name=" + name + ", parentid="
				+ parentid + ", nodepath=" + nodepath + ", namepath="
				+ namepath + ", charindex=" + charindex + ", level=" + level
				+ ", orderby=" + orderby + "]";
	}

	/*
	 * 1.重写equals方法修饰符必须是public,因为是重写的Object的方法. 2.参数类型必须是Object.
	 */
	public boolean equals(Object other) {
		if (this == other)
			return true;
		if (other == null)
			return false;
		if (!(other instanceof District))
			return false;

		final District cat = (District) other;

		if (!getName().equals(cat.getName()))
			return false;
		if (!getId().equals(cat.getId()))
			return false;
		return true;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the parentid
	 */
	public String getParentid() {
		return parentid;
	}

	/**
	 * @return the nodepath
	 */
	public String getNodepath() {
		return nodepath;
	}

	/**
	 * @return the namepath
	 */
	public String getNamepath() {
		return namepath;
	}

	/**
	 * @return the charindex
	 */
	public String getCharindex() {
		return charindex;
	}

	/**
	 * @return the level
	 */
	public String getLevel() {
		return level;
	}

	/**
	 * @return the orderby
	 */
	public String getOrderby() {
		return orderby;
	}

}
